import router from '@/router'
import store from '@/store'

// 路由全局守卫
router.beforeEach((to, from, next) => {
  // 白名单
  const nextTo = ['/login', '/404']
  // 判断有无token
  const token = store.state.user.token
  if (!token) {
    // 如果没有token,查看是否在白名单
    if (nextTo.includes(to.path)) {
      return next()
    } else {
      return next('/login')
    }
  } else {
    // 如果有token,则无法前往登录页面
    if (to.path === '/login') return next('/')
    // 获取左侧菜单权限
    store.dispatch('user/getMenus')
    next()
  }
})
